const bodyParser = require('body-parser')
const { list } = require('./mock')
const telList = [{
	tel: '13111111111',
	name: '张三',
}, {
	tel: '13111111123',
	name: '李四',
}, {
	tel: '13222222222',
	name: '王五',
}]

module.exports = app => {
	app.use(bodyParser.json())

	app.post('/api/tel/signup', (req, res) => {
		const { tel } = req.body;
		const curr = telList.find(v => v.tel === tel);

		if (curr) {
			res.send({
				code: 200,
				data: curr,
				mess: '登录成功, 欢迎' + curr.name
			})
		} else {
			res.send({
				code: 404,
				data: null,
				mess: '该手机号未注册， 请注册'
			})
		}
	})


	app.get('/api/list', (req, res) => {
		res.send({
			code: 200,
			data: list
		})
	})

	app.post('/api/detail', (req, res) => {
		const { id } = req.body
		const curr = list.find(v => v.id === id)
		if(curr) {
			res.send({
				code: 200,
				data: curr
			})
		} else {
			res.send({
				code: 404,
				data: null,
				mess: '商品下架'
			})
		}
	})
}